﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mail;
using System.Net.Mail;
using System.Net;
using System.Text;
using XQ.Framework.model;
using XQ.Framework.core;

namespace XQ.Framework.net
{
    /// <summary>
    ///Send_Emails 的摘要说明
    /// </summary>
    public class EmailSender
    {
        //发送设置
        private EmailModelBase model { get; set; }

        public EmailSender(EmailModelBase email)
        {
            model = email;
        }

        private bool checkSetting()
        {
            if (model == null)
            {
                return false;
            }
            if (!ValidateHelper.IsAllPlumpString(model.To, model.SmtpServer,
                model.UserName, model.Password)
                || !ValidateHelper.IsEmail(model.To))
            {
                return false;
            }
            return true;
        }

        private System.Net.Mail.MailMessage BuildMail(EmailModelBase model)
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            //收件人
            mail.To.Add(model.To);
            if (model.ToList != null && model.ToList.Count > 0)
            {
                model.ToList.ForEach(delegate(string to)
                {
                    if (ValidateHelper.IsEmail(to))
                    {
                        mail.To.Add(to);
                    }
                });
            }
            //抄送人
            if (model.CcList != null && model.CcList.Count > 0)
            {
                model.CcList.ForEach(delegate(string cc)
                {
                    if (ValidateHelper.IsEmail(cc))
                    {
                        mail.CC.Add(cc);
                    }
                });
            }
            mail.From = new MailAddress(model.Address, model.SenderName, Encoding.UTF8);
            mail.Subject = model.Subject;
            mail.SubjectEncoding = Encoding.UTF8;
            mail.Body = model.MailBody;
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;//发送html
            mail.Priority = System.Net.Mail.MailPriority.Normal;
            return mail;
        }

        private SmtpClient BuildSmtp(EmailModelBase model)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Credentials = new NetworkCredential(model.UserName, model.Password);
            smtp.Host = model.SmtpServer;
            smtp.EnableSsl = model.EnableSSL;
            smtp.Port = model.SendPort;
            smtp.Timeout = model.TimeOut;
            return smtp;
        }

        public bool sendMail()
        {
            System.Net.Mail.MailMessage mail = null;
            SmtpClient smtp = null;
            try
            {
                if (!this.checkSetting()) { return false; }
                mail = BuildMail(model);
                smtp = BuildSmtp(model);
                smtp.Send(mail);
                return true;
            }
            catch (Exception e)
            { return false; }
            finally
            {
                if (smtp != null) { smtp.Dispose(); }
                if (mail != null) { mail.Dispose(); }
            }
        }
    }
}